package com.summer.safe.config.aop.noRepeat;
import com.google.common.cache.Cache;
import com.summer.safe.config.annotation.NoRepeatSubmit;
import com.summer.safe.config.result.ResultCode;
import com.summer.safe.config.result.ResultWrapperEntity;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

@Aspect
@Component
/**
 * @功能描述 aop解析注解
 */
public class NoRepeatSubmitAspect {

    @Resource
    private MappingJackson2HttpMessageConverter converter;
    private Log logger = LogFactory.getLog(getClass());

    @Resource
    private Cache<String, Integer> cache;


    @Around("execution(* com.summer.safe..*Controller.*(..)) && @annotation(nrs)")
    public Object around(ProceedingJoinPoint pjp, NoRepeatSubmit nrs) throws Throwable {

        ResultWrapperEntity wrapperEntity = new ResultWrapperEntity();

        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        HttpOutputMessage outputMessage = new ServletServerHttpResponse(response);

        try {
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            String sessionId = Objects.requireNonNull(RequestContextHolder.getRequestAttributes()).getSessionId();
            HttpServletRequest request;
            if (attributes != null) {
                request = attributes.getRequest();
                String key = sessionId + "-" + request.getServletPath();
                // 如果缓存中有这个url视为重复提交
                if (cache.getIfPresent(key) == null) {
                    Object o = pjp.proceed();
                    cache.put(key, 0);
                    return o;
                } else {
                    logger.error("重复提交");
                    wrapperEntity = new ResultWrapperEntity(ResultCode.REPEAT_SUBMIT);
                    converter.write(wrapperEntity, MediaType.APPLICATION_JSON, outputMessage);
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
            logger.error("验证重复提交时出现未知异常!");
            wrapperEntity = new ResultWrapperEntity(ResultCode.REPEAT_SUBMIT_ERROR);
            converter.write(wrapperEntity, MediaType.APPLICATION_JSON, outputMessage);
        }
        return null;
    }

}